/* AccountType.cpp
   Michael Zahniser, 2008.

   Function definitions for the AccountType enumeration class.
*/

#include "AccountType.h"



// Names of the different account types. Keep this array synchronized with the
// enumeration constants below.
const string AccountType::NAME[] = {"Account", "In", "Out", "Virtual"};

// An "ACCOUNT" is the default account type. It represents money that is
// saved in some account.
const AccountType AccountType::ACCOUNT(0);
// An "IN" account represents a source of income.
const AccountType AccountType::IN(1);
// An "OUT" account is a category of spending.
const AccountType AccountType::OUT(2);
// A "VIRTUAL" account is one that exists only to represent money that
// appears "out of thin air" - for example, the initial funds that are
// in an account before the transaction record begins, changes in value
// of investments, etc.
const AccountType AccountType::VIRTUAL(3);



AccountType::AccountType()
    : type(ACCOUNT.type)
{
}



AccountType::AccountType(const AccountType &accountType)
{
    *this = accountType;
}



AccountType::AccountType(const string &name)
{
    *this = name;
}



AccountType &AccountType::operator=(const AccountType &accountType)
{
    type = accountType.type;
    return *this;
}



AccountType &AccountType::operator=(const string &name)
{
    type = 0;
	unsigned nameCount = sizeof(NAME) / sizeof(NAME[0]);
    for(unsigned i = 0; i < nameCount; ++i)
	if(NAME[i] == name)
	    type = i;
    
    return *this;
}



// Equality check.
bool AccountType::operator==(const AccountType &accountType) const
{
    return (type == accountType.type);
}



bool AccountType::operator!=(const AccountType &accountType) const
{
    return !(*this == accountType);
}



// Get the name of this account type.
const string &AccountType::Name() const
{
    return NAME[type];
}



// Return true if a negative balance is a _good_ thing (i.e. income).
bool AccountType::IsInverted() const
{
    return (*this == IN) | (*this == VIRTUAL);
}



// Check whether accounts of this type can logically have their balances
// displayed as a percentage of a total of accounts of the given type.
bool AccountType::IsCompatible(const AccountType &accountType) const
{
    // Expenditures can be displayed as a percentage of income.
    // The reverse, however, does not make sense.
    return (*this == accountType) | ((*this == OUT) & (accountType == IN));
}



// Private constructor:
AccountType::AccountType(int type)
    : type(type)
{
}
